package com.funtl.screw.admin.controller;

import cn.hutool.core.lang.tree.Tree;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.funtl.screw.admin.entity.SysDept;
import com.funtl.screw.admin.entity.SysDeptRelation;
import com.funtl.screw.admin.service.SysDeptRelationService;
import com.funtl.screw.admin.service.SysDeptService;
import com.funtl.screw.common.core.util.ResponseResult;
import com.funtl.screw.common.log.annotation.SysLog;
import io.swagger.annotations.Api;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 部门管理 前端控制器
 * </p>
 *
 * @author Lusifer
 * @since 2018-01-20
 */
@RestController
@AllArgsConstructor
@RequestMapping("/dept")
@Api(value = "dept", tags = "部门管理模块")
public class SysDeptController {

    private final SysDeptRelationService relationService;

    private final SysDeptService sysDeptService;

    /**
     * 通过ID查询
     *
     * @param id ID
     * @return SysDept
     */
    @GetMapping("/{id}")
    public ResponseResult<SysDept> getById(@PathVariable Long id) {
        return ResponseResult.ok(sysDeptService.getById(id));
    }

    /**
     * 查询全部部门
     */
    @GetMapping("/list")
    public ResponseResult<List<SysDept>> list() {
        return ResponseResult.ok(sysDeptService.list());
    }

    /**
     * 返回树形菜单集合
     *
     * @param deptName 部门名称
     * @return 树形菜单
     */
    @GetMapping(value = "/tree")
    public ResponseResult<List<Tree<Long>>> getTree(String deptName) {
        return ResponseResult.ok(sysDeptService.selectTree(deptName));
    }

    /**
     * 添加
     *
     * @param sysDept 实体
     * @return success/false
     */
    @SysLog("添加部门")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('sys_dept_add')")
    public ResponseResult<Boolean> save(@Valid @RequestBody SysDept sysDept) {
        return ResponseResult.ok(sysDeptService.saveDept(sysDept));
    }

    /**
     * 删除
     *
     * @param id ID
     * @return success/false
     */
    @SysLog("删除部门")
    @DeleteMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('sys_dept_del')")
    public ResponseResult<Boolean> removeById(@PathVariable Long id) {
        return ResponseResult.ok(sysDeptService.removeDeptById(id));
    }

    /**
     * 编辑
     *
     * @param sysDept 实体
     * @return success/false
     */
    @SysLog("编辑部门")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('sys_dept_edit')")
    public ResponseResult<Boolean> update(@Valid @RequestBody SysDept sysDept) {
        sysDept.setUpdateTime(LocalDateTime.now());
        return ResponseResult.ok(sysDeptService.updateDeptById(sysDept));
    }

    /**
     * 查收子级列表
     *
     * @return 返回子级
     */
    @GetMapping(value = "/getDescendantList/{deptId}")
    public ResponseResult<List<SysDeptRelation>> getDescendantList(@PathVariable Long deptId) {
        return ResponseResult.ok(relationService.list(Wrappers.<SysDeptRelation>lambdaQuery().eq(SysDeptRelation::getAncestor, deptId)));
    }
}
